package Queue;

/**
 * ClassName: ArrayQueue
 * Package: Queue
 *
 * @Author 马学兴
 * @Create 2023/12/4 13:00
 * @Version 1.0
 * Description:队列-先进先出
 */
public class ArrayQueue <E> implements Queue<E>{
    private Array<E> array;

    public ArrayQueue(int capacity) {
        array= new Array<E>(capacity);
    }
    public ArrayQueue() {
        array= new Array<E>();
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }
    public int getCapacity(){
        return array.getCapacity();
    }

    @Override
    public void enqueue(E e) {//入队
        array.addLast(e);
    }

    @Override
    public E dequeue() {
        return array.removeFirst();
    }

    @Override
    public E getFront() {//查看队首
        return array.getFirst();
    }
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Queue: ");
        res.append("front [");
        for (int i = 0; i < array.getSize(); i++) {
            res.append(array.get(i));
            if (i!=array.getSize()-1){
                res.append(",");
            }
        }
        res.append("] tail");
        return res.toString();
    }

    public static void main(String[] args) {
        ArrayQueue<Integer> queue = new ArrayQueue<Integer>();
        for (int i = 0; i < 5; i++) {
            queue.enqueue(i);
            System.out.println(queue);
            if (i % 3 == 2) {
                queue.dequeue();
                System.out.println(queue);
            }
        }
    }
}
